<!--
 * @Author: 李天惊 uf_lee@163.com
 * @Date: 2022-12-20 14:35:28
 * @LastEditors: 李天惊 uf_lee@163.com
 * @LastEditTime: 2023-01-04 09:36:21
 * @FilePath: \grazlink20\uc_sys\src\views\Asset\Pasture\components\Edit.vue
 * Copyright (c) 2023 by 李天惊 uf_lee@163.com, All Rights Reserved. 
-->
<script setup lang="ts">
import { reactive, ref } from 'vue'
import { Form } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { adminFaPastureSeeApi } from '@/api/fa/pasture'
import { adminFaUserListApi } from '@/api/fa/user'
import { required } from '@/utils/formRules'

// 获取操作员
const getOperator = async (new_data) => {
  const res = await adminFaUserListApi({ ...new_data, source: 'operator' })
    .catch(() => {})
    .finally(() => {})
  if (res) {
    return res
  }
}

/****************************** 变量 start ******************************/
const data = ref<any>({})

const rules = reactive({
  org_name: [required]
})

const schema: any = [
  {
    field: 'id',
    label: '主键',
    component: 'Input',
    componentProps: {
      disabled: true
    }
  },
  {
    field: 'operator_id',
    label: '关联操作员',
    component: 'DialogRefer',
    componentProps: {
      getListApi: getOperator,
      response: { count: 'count', data: 'data' },
      title: '选择操作员（双击选择）',
      nameField: 'operator_name',
      relNameField: 'nickname',
      valueField: 'id',
      crudSchemas: [
        { field: 'nickname', label: '姓名', search: { show: true } },
        { field: 'username', label: '用户名', search: { show: true } },
        { field: 'source', label: '类型' }
      ],
      modelName: data.value.operator_name
    }
  },
  {
    field: 'code',
    label: '公司编码',
    component: 'Input'
  },
  {
    field: 'name',
    label: '客户名称',
    component: 'Input'
  },
  {
    field: 'phone',
    label: '客户电话',
    component: 'Input'
  },
  {
    field: 'postal_code',
    label: '邮政编码',
    component: 'Input'
  },
  {
    field: 'jurisdiction',
    label: '所属辖区',
    component: 'Input'
  },
  {
    field: 'address',
    label: '客户地址',
    component: 'Input'
  },
  {
    field: 'unified_code',
    label: '统一社会信用代码',
    colProps: {
      span: 24
    },
    component: 'Input'
  },
  {
    field: 'business_license',
    label: '营业执照',
    colProps: {
      span: 24
    },
    component: 'Upload'
  },
  {
    field: 'legal_name',
    label: '法人姓名',
    component: 'Input'
  },
  {
    field: 'legal_phone',
    label: '法人电话',
    component: 'Input'
  },
  {
    field: 'legal_address',
    label: '法人地址',
    component: 'Input'
  },
  {
    field: 'legal_identity_code',
    label: '法人身份证号码',
    component: 'Input'
  },
  {
    field: 'legal_identity',
    label: '法人身份证',
    colProps: {
      span: 24
    },
    component: 'Upload'
  },
  {
    field: 'owner_name',
    label: '所有权人名称',
    component: 'Input'
  },
  {
    field: 'owner_identity_code',
    label: '所有权人身份证号码',
    component: 'Input'
  },
  {
    field: 'owner_identity',
    label: '所有权人身份证',
    colProps: {
      span: 24
    },
    component: 'Upload'
  },
  {
    field: 'other_name',
    label: '其他联系人名称',
    component: 'Input'
  },
  {
    field: 'other_phone',
    label: '其他联系人电话',
    component: 'Input'
  },
  {
    field: 'save_total',
    label: '存栏数（总）',
    component: 'Input'
  },
  {
    field: 'save_register',
    label: '存栏数（已登记）',
    component: 'Input'
  },
  {
    field: 'save_marking',
    label: '存栏数（已打标）',
    component: 'Input'
  },
  {
    field: 'save_mortgage',
    label: '存栏数（已抵押）',
    component: 'Input'
  },
  {
    field: 'save_insure',
    label: '存栏数（已投保）',
    component: 'Input'
  },
  {
    field: 'creation_time',
    label: '创建时间',
    component: 'DatePicker',
    componentProps: {
      disabled: true,
      valueFormat: 'YYYY-MM-DD HH:mm:ss'
    }
  },
  {
    field: 'creation_user_id',
    label: '创建人',
    component: 'DialogRefer',
    componentProps: {
      getListApi: adminFaUserListApi,
      response: { count: 'count', data: 'data' },
      title: '选择创建人（双击选择）',
      nameField: 'creation_user_name',
      relNameField: 'nickname',
      valueField: 'id',
      crudSchemas: [
        { field: 'nickname', label: '姓名', search: { show: true } },
        { field: 'username', label: '用户名', search: { show: true } },
        { field: 'source', label: '类型' }
      ],
      modelName: data.value.creation_user_name
    }
  }
]

const { register, methods, elFormRef } = useForm({
  schema
})

/****************************** 变量 end ******************************/

/****************************** 组件参数 start ******************************/
// 获取参数
const props = defineProps({
  id: {
    type: String,
    default: null
  }
})
/****************************** 组件参数 end ******************************/

/****************************** 函数方法 start ******************************/
const getPastureSee = async () => {
  if (props.id) {
    const res = await adminFaPastureSeeApi({ id: props.id })
      .catch(() => {})
      .finally(() => {})
    if (res) {
      data.value = res.data
      await methods.setValues(data.value)
    }
  }
}

/****************************** 函数方法 end ******************************/

/****************************** 返回参数 start ******************************/
defineExpose({
  elFormRef,
  getFormData: methods.getFormData
})
/****************************** 返回参数 end ******************************/

/****************************** 初始化 start ******************************/
const init = async () => {
  // 调用查看接口查询数据
  await getPastureSee()
}
init()
/****************************** 初始化 end ******************************/
</script>

<template>
  <Form :rules="rules" @register="register" :data="data" />
</template>
